package com.example.lanqiaodemo1;

import java.io.*;
import java.util.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class Main {
    static ArrayList<Student> studentsList = new ArrayList<>();
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws Exception {
        init();
        beginProject();
    }

    public static void init() throws Exception {
        ObjectInputStream inputStream = null;
        try {
            inputStream = new ObjectInputStream(new FileInputStream("D:\\Student.txt"));
            studentsList = (ArrayList<Student>) inputStream.readObject();
        } catch (EOFException e) {
        }
    }

    public static void beginProject() throws Exception {
        String chose;
        do {
            showMenu();
            System.out.println("选择菜单");
            chose = scanner.next();
            switch (chose) {
                case "1":
                    getUserInformation();
                    break;
                case "2":
                    showStudentInformation();
                    break;
                case "3":
                    getStudentInformationByIdDesc();
                    break;
                case "4":
                    getStudentInfoById();
                    break;
                case "5":
                    getStudentInfoByName();
                    break;
                case "6":
                    getAllStudentSumScoreAndAverageScore();
                    break;
                case "7":
                    getStudentInfoByAverageDesc();
                    break;
                case "9":
                    break;
            }
        } while (!chose.equals("8"));
        ObjectOutputStream outputStream = null;
        outputStream = new ObjectOutputStream(new FileOutputStream("D:\\Student.txt"));
        outputStream.writeObject(studentsList);
    }

    public static void showMenu() {
        System.out.println("1.添加学生信息");
        System.out.println("2.查看所有学生信息");
        System.out.println("3.查看学生信息 按照学号降序排序");
        System.out.println("4.根据学号查询学生信息");
        System.out.println("5.根据学生姓名查询学生的信息");
        System.out.println("6.获取每一个学生的总分和");
        System.out.println("7.按照学生的平均分降序排列集合中的数据");
        System.out.println("8.结束程序");
    }


    public static void getUserInformation() {
        Student student = new Student();
        System.out.println("请输入学号");
        long id = scanner.nextLong();
        student.setId(id);
        System.out.println("请输入学生姓名");
        String name = scanner.next();
        student.setName(name);
        System.out.println("请输入学生班级");
        String classRoom = scanner.next();
        student.setClassRoom(classRoom);
        System.out.println("请输入学生的性别");
        String sex = scanner.next();
        student.setSex(sex);
        System.out.println("请输入学科数量:");
        int n = scanner.nextInt();
        for (int j = 0; j < n; j++) {
            System.out.println("请输入要录入的学生的学科:");
            String ClassName = scanner.next();
            System.out.println("请输入学生的成绩:");
            double studentScore = scanner.nextDouble();
            student.studentScore.add(student.new score(n, ClassName, studentScore));
        }
        studentsList.add(student);
        System.out.println("录入信息成功 请进行下一步操作");
    }

    public static void showStudentInformation() {
        Collections.sort(studentsList, (o1, o2) -> (int) (o1.getId() - o2.getId()));
        for (Student student : studentsList) {
            System.out.println(student);
        }
    }

    public static void getStudentInformationByIdDesc() {
        Collections.sort(studentsList, (o1, o2) -> (int) (o2.getId() - o1.getId()));
        for (Student student : studentsList) {
            System.out.println(student);
        }
    }

    public static void getStudentInfoById() {
        System.out.println("请输入你要查询的id");
        Scanner scanner = new Scanner(System.in);
        long id = scanner.nextLong();

        for (Student student : studentsList) {
            if (id == student.getId()) {
                System.out.println(student);
                return;
            }
        }
        System.out.println("根据id查不到此人，请检查你输入的id");
    }

    public static void getStudentInfoByName() {
        System.out.println("请输入用户的姓名");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        for (Student student : studentsList) {
            if (student.getName().equals(name)) {
                System.out.println(student);
                return;
            }
        }
        System.out.println("根据姓名查不到此人，请检查你输入的姓名");
    }

    public static void getAllStudentSumScoreAndAverageScore() throws Exception {
        for (Student student : studentsList) {
            int sum = 0;
            int average = 0;
            for (int i = 0; i < student.getStudentScore().size(); i++) {
                sum += student.studentScore.get(i).getClassScore();
            }
            System.out.println(student.getName() + "的总分是" + sum + "  " + student.getName() + "的平均分是" + sum / student.getStudentScore().size());
        }
    }

    public static void getStudentInfoByAverageDesc() {
        Collections.sort(studentsList, (o1, o2) -> (int) (o2.getAverageScore() - o1.getAverageScore()));
        for (Student student : studentsList) {
            System.out.println(student);
        }
    }
}



class Student implements Serializable {
    private Long id;
    private String name;
    private String sex;
    private String classRoom;
    public List<score> studentScore;

    public Student(Long id, String name, String sex, String classRoom, List<score> studentScore) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.classRoom = classRoom;
        this.studentScore = studentScore;
    }

    public Student() {
        studentScore = new ArrayList<>();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getClassRoom() {
        return classRoom;
    }

    public void setClassRoom(String classRoom) {
        this.classRoom = classRoom;
    }

    public List<score> getStudentScore() {
        return studentScore;
    }

    public void setStudentScore(List<score> studentScore) {
        this.studentScore = studentScore;
    }

    //平均值
    public double getAverageScore() {
        double sum = 0;
        for (int i = 0; i < studentScore.size(); i++) {
            sum += studentScore.get(i).getClassScore();
        }
        return sum / (studentScore.size());
    }

    @Override
    public String toString() {
        return "Student{" + "id=" + id + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", classRoom='" + classRoom + '\'' + ", studentScore=" + studentScore + '}';
    }

    //SCORE 学生成绩内部类
    public class score implements Serializable {
        private int n;
        private String ClassName;
        private double ClassScore;

        public score(int n, String className, double classScore) {
            this.n = n;
            ClassName = className;
            ClassScore = classScore;
        }

        public int getN() {
            return n;
        }

        public void setN(int n) {
            this.n = n;
        }

        public String getClassName() {
            return ClassName;
        }

        public void setClassName(String className) {
            ClassName = className;
        }

        public double getClassScore() {
            return ClassScore;
        }

        public void setClassScore(double classScore) {
            ClassScore = classScore;
        }

        @Override
        public String toString() {
            return "score{" +
                    "n=" + n +
                    ", ClassName='" + ClassName + '\'' +
                    ", ClassScore=" + ClassScore +
                    '}';
        }
    }
}